<template>
  <view class="">
    <u-navbar :fixed="true" :placeholder="true" title="文件预览" @leftClick="leftClick" bgColor="#0F53AC"
      leftIconColor="#fff">
    </u-navbar>
    <web-view :src="url"></web-view>
  </view>

</template>

<script>
  export default {
    data() {
      return {
        params: {},
        url: ''
      }
    },
    onLoad(options) {
      // this.params = JSON.parse(options.params)
      // uni.$u.http.get(`/base/upload/bindFileUrlByFileName?fileUrl=${options.fileUrl}&fileName=${'文件'}`).then(res => {
      //   if (res.code == 0) {
      //     this.openFile(res.data)
      //   }
      // })
      // this.url = 'https://zdygrouptest.douziit.com/openFile.html?fileUrl=' + encodeURI(options.url)
      this.url = encodeURI(options.url)
    },
    methods: {
      leftClick() {
        uni.navigateBack({
          delta: 1
        })
      },
      openFile(url) {
        uni.downloadFile({
          url: url,
          success: function(res) {
            console.log(res);
            var filePath = res.tempFilePath;
            var i = filePath.lastIndexOf('.')
            var type = filePath.slice(i, filePath.length)
            if (type === '.jpg' || type === '.jpeg' || type === '.png' || type === '.gif') {
              //如果是图片走这里
              uni.previewImage({
                current: 0,
                urls: [url],
              })
            } else {
              //如果是文件走这里
              uni.openDocument({
                filePath: filePath,
                success: (res) => console.log('成功打开文档'),
                fail: (res) => {
                  uni.showToast({
                    icon: 'none',
                    title: '文件打开失败',
                    duration: 2000
                  })
                }
              })
            }
          }
        });
      }
    }
  }
</script>

<style>
</style>